package wiki.zimo.dao;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import wiki.zimo.domain.Orderitem;

import java.sql.SQLException;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/5 17:49
 * @description: OrderItemDao数据库操作类
 */
public class OrderItemDao extends BaseDao<Orderitem> {

    /**
     * @auther: 子墨
     * @date: 2018/11/6 22:49
     * @describe: 根据订单id查找订单项集
     * @param: [oid]
     * @return: java.util.List<wiki.zimo.domain.Orderitem>
     * @version v1.0
     */
    public List<Orderitem> findOrderitemsByOid(String oid) throws SQLException {
        String sql = "select * from orderitem where oid = ?";
        return qr.query(sql, new BeanListHandler<>(Orderitem.class), oid);
    }
}
/*public class OrderItemDao implements Dao<Orderitem,String> {

	// 获取SQL语句的内部类
	class SQL extends SQLUtils<Orderitem> {

	}
	// 获取SQL语句的对象
	SQL s = new SQL();

	@Override
	public Orderitem insert(Orderitem entity) throws SQLException {
//		String sql = "insert into Orderitem values ( ?, ?, ?, ?, ? )";
		String sql = s.getInsertSql();
		return qr.insert(sql,
				new BeanHandler<>(Orderitem.class),
				entity.getItemid(),
				entity.getPid(),
				entity.getOid(),
				entity.getCount(),
				entity.getSubTotal()
		);
	}

	@Override
	public boolean delete(Orderitem entity) throws SQLException {
//		String sql = "delete from Orderitem where itemid = ?";
		String sql = s.getDeleteSql();
		int update = qr.update(sql, entity.getItemid());
		if (update > 0){
			return true;
		}
		return false;
	}

	@Override
	public Orderitem update(Orderitem entity) throws SQLException {
//		String sql = "update Orderitem set pid = ?,oid = ?,count = ?,subtotal = ? where itemid = ?";
		String sql = s.getUpdateSql();
		int update = qr.update(sql,
				entity.getPid(),
				entity.getOid(),
				entity.getCount(),
				entity.getSubTotal(),
				entity.getItemid()
		);
		if (update > 0){
			return findOneById(entity.getItemid());
		}
		return null;
	}

	@Override
	public Orderitem findOneById(String id) throws SQLException {
//		String sql = "select * from Orderitem where itemid = ? limit 1";
		String sql = s.getFindOneByIdSql();
		return qr.query(sql,new BeanHandler<>(Orderitem.class),id);
	}

	@SuppressWarnings("NoUse")
	@Override
	public Orderitem findOneByParams(String... params) throws SQLException {
		return null;
	}

	@Override
	public List<Orderitem> findAll() throws SQLException {
//		String sql = "select * from Orderitem";
		String sql = s.getFindAllSql();
		return qr.query(sql,new BeanListHandler<>(Orderitem.class));
	}

	@Override
	public int count() throws SQLException {
		return findAll().size();
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/6 22:49
 * @describe: 根据订单id查找订单项集
 * @param: [oid]
 * @return: java.util.List<wiki.zimo.domain.Orderitem>
 * @version v1.0
 *//*
	public List<Orderitem> findOrderitemsByOid(String oid) throws SQLException {
		String sql = "select * from orderitem where oid = ?";
		return qr.query(sql,new BeanListHandler<>(Orderitem.class),oid);
	}
}*/
